<?php
/**
 * ClassName:渠道处理器
 * @auditor:leijunxiang
 * @time 2025/4/2 10:25
 */

namespace app\tool\freightForward;

use app\model\freightForward\Channel;
use app\modelOperate\freightForward\ChannelOpe;
use app\modelOperate\freightForward\FreightForwardOpe;
use app\tool\ToolBase;
use think\facade\Log;

class ChannelHandle extends ToolBase
{
    /**
     * 新增渠道(货代id,渠道名称)
     */
    public function addChannel($id, $name)
    {
        $this->init();
        //(true/false)货代id校验(货代id)
        $flag = $this->freightForwardVerify($id);
        if (!$flag) {
            $this->errCode = FAIL;
            $this->errMsg = "未找到货代，请先创建";
            return $flag;
        }
        $operate = app(ChannelOpe::class);
        $obj = Channel::getObj();
        $channel = $name;
        //渠道名称
        $obj->setchannelName($channel);
        $forwardId = $id;
        //货代id
        $obj->setforwardId($forwardId);
        $flag = $operate->saveData($obj->getAttribute());
        if (!$flag) {
            $this->errCode = FAIL;
            $this->errMsg = "数据保存失败";
        }
        return $flag ? true : false;
    }

    /**
     * 查询渠道(货代id,页码,条数)
     */
    public function selectChannel($id, $page, $limit)
    {
        $this->init();
        $ope = app(ChannelOpe::class);
        //根据id查询数据
        $data = $ope->selectDataByForwardId($id, $page, $limit);
        //详情结果处理(详情数据)
        $data['totalCount'] = $this->getCount($id, $page);
        if (empty($data)) {
            $this->errCode = FAIL;
            $this->errMsg = "未找到渠道";
        }
        return $data;
    }

    /**
     * 修改渠道(数据)
     */
    public function updateChannel($channelId,$channelName)
    {
        $this->init();
        try {
            $operate = app(ChannelOpe::class);
            $flag = $operate->updateChannelData($channelId,$channelName);
            if ($flag) {
                $this->setErrCode(SUCCESS);
                $this->setErrMsg("修改成功");
            } else {
                $this->setErrCode(FAIL);
                $this->setErrMsg("修改失败");
            }
        } catch (\Exception $e) {
            $this->errCode = FAIL;
            $this->errMsg = $e->getMessage();
        }
        return $this->isGoOn();
    }

    /**
     * 删除渠道(渠道id数组)
     */
    public function delChannel($arr)
    {
        $this->init();
        try {
            $operate = app(ChannelOpe::class);
            if($arr){
                foreach ($arr as $item) {
                    $flag = $operate->delData($item);
                    if (!$flag) {
                        $this->setErrCode(FAIL);
                        $this->setErrMsg("删除失败");
                        break;
                    }
                }
            }
        } catch (\Exception $e) {
            $this->errCode = FAIL;
            $this->errMsg = $e->getMessage();
        }
        return $this->isGoOn();
    }


    /**
     * (true/false)货代id校验(货代id)
     */
    protected function freightForwardVerify($id)
    {
        $data = FreightForwardOpe::find($id);
        if (empty($data)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * (总数)获取总数(渠道id,页数)
     */
    protected function getCount($channelId, $page)
    {
        if ($page > 1) {
            return 0;
        }
        $ope = app(ChannelOpe::class);
        //根据渠道id查询数量
        $count = $ope->selectCount($channelId);
        return $count;
    }

}